﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TMI85303.Utils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;


namespace TMI85303.UI.UserControls
{
    /// <summary>
    /// StatusControl.xaml 的交互逻辑
    /// </summary>
    public partial class StatusControl : UserControl
    {
        public StatusControl()
        {
            InitializeComponent();
            
        }

    }

    public partial class StatusV2VM : ObservableObject
    {
        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit0 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit1 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit2 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit3 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit4 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit5 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit6 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit7 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit8 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg147Bit9 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg148Bit0_Bit3_Bit5 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg148Bit1 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg148Bit2 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg148Bit4 = ColorsHelper.Green;

        [ObservableProperty]
        private SolidColorBrush foregroundReg148Bit6 = ColorsHelper.Green;

        [ObservableProperty]
        private double die_temp_C = double.MinValue;

        [ObservableProperty]
        private double die_temp_F = double.MinValue;

        [RelayCommand]
        private void Clear_Default()
        {
            ReadWrite.WriteReg(148, 0x8000, 2);
            ReadWrite.WriteReg(148, 0, 2);
            GetFaults();
        }

        [RelayCommand]
        private void ReadReg()
        {
            int readData138 = ReadWrite.ReadReg(138, 2);
            Die_temp_C = Math.Round(3.0 + (readData138 - 133.0) / 2, 1);
            Die_temp_F = Math.Round(1.8 * (double)Die_temp_C + 32.0, 1);
            GetFaults();
        }

        public void GetFaults()
        {
            int data = ReadWrite.ReadReg(147, 2);
            ForegroundReg147Bit0 = ((data & 1) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit1 = ((data & 2) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit2 = ((data & 4) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit3 = ((data & 8) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit4 = ((data & 0x10) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit5 = ((data & 0x20) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit6 = ((data & 0x40) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit7 = ((data & 0x80) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit8 = ((data & 0x100) == 0) ? ColorsHelper.Green : ColorsHelper.Red;
            ForegroundReg147Bit9 = ((data & 0x200) == 0) ? ColorsHelper.Green : ColorsHelper.Red;

            data = ReadWrite.ReadReg(148, 2);
            ForegroundReg148Bit0_Bit3_Bit5 = ((((data & 1) | (data & 8) | (data & 0x20)) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            ForegroundReg148Bit1 = (((data & 2) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            ForegroundReg148Bit2 = (((data & 4) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            //label_reg148Bit0_Bit3_Bit5.ForeColor = (((data & 8) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            ForegroundReg148Bit4 = (((data & 0x10) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            //label_reg148Bit0_Bit3_Bit5.ForeColor = (((data & 0x20) == 0) ? ColorsHelper.Green : ColorsHelper.Red);
            ForegroundReg148Bit6 = (((data & 0x40) == 0) ? ColorsHelper.Green : ColorsHelper.Red);

        }

    }

}
